/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.EnumProperty;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bus.entity.Audit;
import com.thinkgem.jeesite.modules.bus.entity.Ent;
import com.thinkgem.jeesite.modules.bus.entity.EntHistory;
import com.thinkgem.jeesite.modules.bus.service.AuditService;
import com.thinkgem.jeesite.modules.bus.service.EntHistoryService;
import com.thinkgem.jeesite.modules.bus.service.EntService;
import com.thinkgem.jeesite.modules.sys.entity.Attachment;
import com.thinkgem.jeesite.modules.sys.service.AttachmentService;

/**
 * 养殖企业信息Controller
 * @author mengying
 * @version 2019-05-30
 */
@Controller
@RequestMapping(value = "${adminPath}/bus/entAudit")
public class AuditController extends BaseController {

	@Autowired
	private AuditService auditService;
	@Autowired
	private EntService entService;
	@Autowired
	private AttachmentService attachmentService;
	@Autowired
	private EntHistoryService entHistoryService;
	
	@ModelAttribute
	public Audit get(@RequestParam(required=false) String id) {
		Audit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = auditService.get(id);
		}
		if (entity == null){
			entity = new Audit();
		}
		return entity;
	}

	@RequiresPermissions("bus:entAudit:view")
	@RequestMapping(value = {"list", ""})
	public String list(Audit entAudit, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Audit> page = auditService.findPage(new Page<Audit>(request, response), entAudit);
		model.addAttribute("page", page);
		return "modules/bus/entAuditList";
	}

	@RequiresPermissions("bus:entAudit:view")
	@RequestMapping(value = "form")
	public String form(Audit entAudit, Model model) {
		model.addAttribute("entAudit", entAudit);
		return "modules/bus/entAuditForm";
	}

	@RequiresPermissions("bus:entAudit:edit")
	@RequestMapping(value = "save")
	public String save(Audit entAudit, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, entAudit)){
			return form(entAudit, model);
		}
		auditService.save(entAudit);
		addMessage(redirectAttributes, "保存养殖企业信息成功");
		return "redirect:"+Global.getAdminPath()+"/bus/entAudit/?repage";
	}
	
	@RequiresPermissions("bus:entAudit:edit")
	@RequestMapping(value = "delete")
	public String delete(Audit entAudit, RedirectAttributes redirectAttributes) {
		auditService.delete(entAudit);
		addMessage(redirectAttributes, "删除养殖企业信息成功");
		return "redirect:"+Global.getAdminPath()+"/bus/entAudit/?repage";
	}
	/**
	 *
	 * @Title: uncheckedList
	 * @author: mengying
	 * @date: 2019/6/1 13:46
	 * @Description: 企业待审核查看页
	 * @param:
	 * @return: String
	 * @throws:
	 */
	@RequiresPermissions("bus:entAudit:view")
//	@RequestMapping(value = "uncheckedList")
	public String uncheckedList(Audit audit,HttpServletRequest request, HttpServletResponse response,Model model) {
		Page<Audit> page = auditService.uncheckedList(new Page<Audit>(request, response),audit);
		model.addAttribute("page", page);
		return "modules/bus/entAuditList";
	}
	
	/**
	 * 
	* @Title: uncheckedEntList 
	* @author: ZYJ
	* @date: 2019年6月22日 上午10:52:42 
	* @Description: 养殖场备案待审核列表
	* @param:  audit
	* @param:  request
	* @param:  response
	* @param:  model
	* @return: String
	* @throws
	 */
	@RequiresPermissions("bus:entAudit:view")
	@RequestMapping(value = "uncheckedList")
	public String uncheckedEntList(Audit audit,HttpServletRequest request, HttpServletResponse response,Model model) {
		Page<Audit> page = auditService.uncheckedEntList(new Page<Audit>(request, response),audit);
		model.addAttribute("page", page);
		return "modules/bus/entAuditList";
	}

	/**
	 *企业已审核展示页面
	 * @author mengying
	 * @param model,HttpServletResponse,HttpServletRequest
	 * @return String
	 */
	@RequiresPermissions("bus:entAudit:view")
	@RequestMapping(value = "checkedList")
	public String checkedLists(Audit audit,HttpServletRequest request, HttpServletResponse response,Model model) {
		Page<Audit> page = auditService.checkedList(new Page<Audit>(request, response),audit);
		model.addAttribute("page", page);
		return "modules/bus/entAuditCheckedList";
	}
	
	/**
	 * 企业审核弹出页面
	 * @author mengying
	 * @param
	 * @param model，entId
	 * @return String
	 */
   // @RequiresPermissions({"bus:entAudit:edit","bus:ent:view"})
    @RequestMapping(value = "check")
    public String check(String auditId, String entId, Model model,String auditStatus,String auditOpinion,String checkFlag) {
    	if(EnumProperty.AuditTypeEnum.UNREVIEWED.getKey().equals(checkFlag)||EnumProperty.AuditTypeEnum.SUCCESS.getKey().equals(checkFlag)){
    		Ent ent=entService.chenckEntInfo(entId);//未审核,已通过--获取企业实时信息
    		model.addAttribute("dicInfoName",ent.getEntDic().getDicInfoName());
    		model.addAttribute("initialDeposit",ent.getEntDic().getInitialDeposit());
			model.addAttribute("yearDeposit",ent.getEntDic().getYearDeposit());
    		model.addAttribute("address",ent.getAddress().getWholeAddr()+ent.getAddress().getDetail());
    		model.addAttribute("ent", ent);
			model.addAttribute("checkFlag", checkFlag);
    	}else{
    		EntHistory ent=entHistoryService.getByAuditId(auditId);//已审核--从历史表获取企业信息
    		model.addAttribute("dicInfoName",ent.getFarmType());
    		model.addAttribute("initialDeposit",ent.getInitialDeposit());
			model.addAttribute("yearDeposit",ent.getYearDeposit());
    		model.addAttribute("address",ent.getWholeAddr());
    		model.addAttribute("ent", ent);
			model.addAttribute("checkFlag", checkFlag);
    	}
    	List<Attachment> attachmentList=attachmentService.attachmentList(entId,"bus_ent");
    	model.addAttribute("attachmentList", attachmentList);
        model.addAttribute("auditStatus", auditStatus);
        model.addAttribute("auditOpinion", auditOpinion);
        return "modules/bus/entCheck";
    }
	/**
	 *修改企业审核状态
	 * @author mengying
	 * @param entId,updateFlag
	 * @return String
	 */
	@RequiresPermissions("bus:entAudit:view")
	@RequestMapping(value = "changeAuditStatus")
	//@ResponseBody
	public String changeAuditStatus(HttpServletResponse response,String entId,String updateFlag,String auditOpinion,String id,RedirectAttributes redirectAttributes) {
		if(auditService.changeAuditStatus(entId,updateFlag,auditOpinion,id)){
			if(EnumProperty.AuditTypeEnum.SUCCESS.getKey().equals(updateFlag)){
				addMessage(redirectAttributes, "审核成功");
			}else{
				addMessage(redirectAttributes, "申请已驳回");
			}
		}else{
			addMessage(redirectAttributes, "审核失败");
		}
		return "redirect:" + Global.getAdminPath() + "/bus/entAudit/uncheckedList/?repage";
	}

}